package com.digiwin.escloud.test.core.common;

import com.digiwin.escloud.test.core.entity.TestCaseEntity;
import org.apache.commons.collections.CollectionUtils;
import org.testng.IAnnotationTransformer;
import org.testng.ISuite;
import org.testng.ISuiteListener;
import org.testng.annotations.IDataProviderAnnotation;
import org.testng.annotations.ITestAnnotation;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @Author yanggld
 * @Date 2020/5/13-16:12
 */
public class CommonTestListener implements ISuiteListener, IAnnotationTransformer {
	TestInitializationContext context = null;
	@Override
	public void onStart(ISuite suite) {
		if (Objects.nonNull(context)) {
			return;
		}
		context = TestInitializationContext.instance();
	}

	@Override
	public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
		Class<?>[] parameterTypes = testMethod.getParameterTypes();
		if (!CollectionUtils.sizeIsEmpty(parameterTypes) && CollectionUtils.size(parameterTypes) == 1) {
			if (Objects.equals(TestCaseEntity.class,parameterTypes[0])) {
				annotation.setDataProvider("dataProvider");
				annotation.setDataProviderClass(CommonDataProvider.class);
			}
		}
	}
}
